package com.tanhua.dubbo.api.impl;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.dubbo.api.SoundApi;
import com.tanhua.dubbo.enums.IdType;
import com.tanhua.dubbo.pojo.Sound;
import com.tanhua.dubbo.service.IdService;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

@DubboService(version = "1.0.0")
public class SoundApiImpl implements SoundApi {

    @Autowired
    private IdService idService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 保存小视频
     *
     * @param sound
     * @return 保存成功后，返回视频id
     */
    @Override
    public String saveSound(Sound sound) {

        //校验数据
        if(!ObjectUtil.isAllNotEmpty(sound.getUserId(),sound.getSoundUrl())) {
            return null;
        }
        //封装sound对象
        sound.setId(ObjectId.get());
        sound.setVid(this.idService.createId(IdType.SOUND));

        //存到mongoDB
        this.mongoTemplate.save(sound);

        return sound.getId().toString();
    }

    @Override
    public Sound querySoundById(String soundId) {
        return this.mongoTemplate.findById(new ObjectId(soundId), Sound.class);
    }
}
